package br.com.gtacomputadores.control.dao;

import br.com.gtacomputadores.control.model.Cliente;
import br.com.gtacomputadores.control.utils.HibernateUtil;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Jeandro
 */
public class ClienteDAOHibernate implements ClienteDAO {

    @Override
    public void save(Cliente cliente) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction t = session.beginTransaction();
      
        session.save(cliente);
        session.flush();
        t.commit();

    }

    @Override
    public Cliente getCliente(long codigo) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        return (Cliente) session.load(Cliente.class, codigo);
    }

    @Override
    public List<Cliente> list() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction t = session.beginTransaction();
        List lista = session.createQuery("from Cliente").list();
        t.commit();
        return lista;
    }

    @Override
    public void remove(Cliente cliente) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction t = session.beginTransaction();
        session.delete(cliente);
        t.commit();
    }

    @Override
    public void update(Cliente cliente) {
       Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction t = session.beginTransaction();
        session.update(cliente);
        session.flush();
        t.commit();

    }

    @Override
    public List<String> buscaFantasia(String busca) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Query query = session.createQuery("from Cliente c where c.nomeFantasia like :busca");
        List list = query.setString("busca", "%"+busca+"%").list();
        return list;
    }

 

    
}
